package cst.a931xmc.yuejian20.Activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import cst.a931xmc.yuejian20.Bean.ActivityBean;
import cst.a931xmc.yuejian20.Bean.ActivityOrderBean;
import cst.a931xmc.yuejian20.Bean.TrainBean;
import cst.a931xmc.yuejian20.Bean.TrainOrderBean;
import cst.a931xmc.yuejian20.Listener.Listener;
import cst.a931xmc.yuejian20.Model.ActivityModel;
import cst.a931xmc.yuejian20.Model.ApiModel;
import cst.a931xmc.yuejian20.Model.TrainModel;
import cst.a931xmc.yuejian20.R;

public class SuccessActivity extends AppCompatActivity implements View.OnClickListener{
    private Context context;
    private int aorder_id=0,torder_id=0;
    private int activity_id,train_id;
    private LinearLayout order_item;
    private ImageView item_pic;
    private TextView item_name;
    private TextView item_intro;
    private TextView order_num;
    private TextView order_price;
    private ImageButton btn_back;
    private Button btn_backhome;
    private ActivityModel model;
    private TrainModel model1;
    private Listener<ActivityOrderBean> listener1;
    private Listener<TrainOrderBean> listener2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_appsuccess);
        init();
        initData();
        if(aorder_id!=0){
            model = new ActivityModel();
            model.getOrderDetail(aorder_id,listener1);
        }
        else if(torder_id!=0){
            model1 = new TrainModel();
            model1.getOrderDetail(torder_id,listener2);
        }
    }

    public void init(){
        order_item = findViewById(R.id.order_item);
        item_pic = findViewById(R.id.item_pic);
        item_name = findViewById(R.id.item_name);
        item_intro = findViewById(R.id.item_intro);
        order_num = findViewById(R.id.order_num);
        order_price = findViewById(R.id.order_price);
        btn_back = findViewById(R.id.btn_back);
        btn_backhome = findViewById(R.id.btn_backhome);
        btn_back.setOnClickListener(this);
        btn_backhome.setOnClickListener(this);
        Intent intent = getIntent();
        aorder_id = intent.getExtras().getInt("aorder_id");
        torder_id = intent.getExtras().getInt("torder_id");
    }

    public void initData(){
        listener1 = new Listener<ActivityOrderBean>() {
            @Override
            public void onResponse(final ActivityOrderBean l) {
                if(l.getAorder_id()!=0){
                    order_num.setText("x" + l.getAorder_num());
                    order_price.setText("￥" + l.getAorder_price());
                    item_name.setText(l.getActivity_name());
                    item_intro.setText(l.getActivity_intro());
                    activity_id = l.getActivity_id();
                    ApiModel apiModel = new ApiModel();
                    Picasso.with(context)
                            .load(apiModel.getBaseUrl() + "static/images/" + l.getActivity_pic())
                            .placeholder(R.drawable.nopic)
                            .into(item_pic);
                    order_item.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(SuccessActivity.this,AorderActivity.class);
                            intent.putExtra("aorder_id",l.getAorder_id());
                            startActivity(intent);
                            finish();
                        }
                    });
                }
                else {
                    Toast.makeText(SuccessActivity.this,"加载失败",Toast.LENGTH_LONG).show();
                }
            }
            @Override
            public void onFail(String message) {
                Toast.makeText(SuccessActivity.this,message, Toast.LENGTH_LONG).show();
            }
        };

        listener2 = new Listener<TrainOrderBean>() {
            @Override
            public void onResponse(final TrainOrderBean l) {
                if(l.getTorder_id()!=0){
                    order_num.setText("x" + l.getTorder_num());
                    order_price.setText("￥" + l.getTorder_price());
                    item_name.setText(l.getTrain_name());
                    item_intro.setText(l.getTrain_intro());
                    train_id = l.getTrain_id();
                    ApiModel apiModel = new ApiModel();
                    Picasso.with(context)
                            .load(apiModel.getBaseUrl() + "static/images/" + l.getTrain_pic())
                            .placeholder(R.drawable.nopic)
                            .into(item_pic);
                    order_item.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(SuccessActivity.this,TorderActivity.class);
                            intent.putExtra("torder_id",l.getTorder_id());
                            startActivity(intent);
                            finish();
                        }
                    });
                }
                else {
                    Toast.makeText(SuccessActivity.this,"加载失败",Toast.LENGTH_LONG).show();
                }
            }
            @Override
            public void onFail(String message) {
                Toast.makeText(SuccessActivity.this,message, Toast.LENGTH_LONG).show();
            }
        };

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_back:
                if(aorder_id!=0){
                    Intent intent = new Intent(SuccessActivity.this,ActivityActivity.class);
                    intent.putExtra("activity_id",activity_id);
                    startActivity(intent);
                    finish();
                }
                else {
                    Intent intent = new Intent(SuccessActivity.this,TrainActivity.class);
                    intent.putExtra("train_id",train_id);
                    startActivity(intent);
                    finish();
                }
                break;
            case R.id.btn_backhome:
                Intent intent = new Intent(SuccessActivity.this,MainActivity.class);
                startActivity(intent);
                finish();
                break;
        }
    }
}
